"""
底层逻辑
"""
import requests

from common.logger import GetLogger


class RequestsClient():
    session = requests.session()

    def __init__(self):
        self.logger = GetLogger.logger
        self.session = RequestsClient.session
        self.json = None
        self.params = None
        self.url = None
        self.method = None
        self.files = None
        self.data = None
        self.headers = None
        self.resp = None

    def request(self,**kwargs):
        if "url" not in kwargs:
            kwargs["url"] = self.url
        if "headers" not in kwargs:
            kwargs["headers"] = self.headers
        if "method" not in kwargs:
            kwargs["method"] = self.method
        if "data" not in kwargs:
            kwargs["data"] = self.data
        if "json" not in kwargs:
            kwargs["json"] = self.json
        if "params" not in kwargs:
            kwargs["params"] = self.params
        if "files" not in kwargs:
            kwargs["files"] = self.files
        # 记录接口相关的请求信息
        for key,value in kwargs.items():
            self.logger.info(f"接口的{key}是:{value}")
        try:
            self.resp = self.session.request(**kwargs)
            self.logger.info(f"接口响应状态码:{self.resp.status_code}")
            self.logger.info(f"接口响应body信息:{self.resp.text}")
        except BaseException as e:
            self.logger.exception("接口调用异常")
            raise BaseException(f"接口调用异常:{e}")
        finally:
            self.logger.info("==============================================================================")
        return self.resp

    # def request(self, **kwargs):
    #     # 注意：并不是所有接口是data参数，也可能是其他参数
    #     # 判断下接口请求的各项信息，在kwargs中有没有，如果没有就用对象自身各个属性
    #     if "url" not in kwargs:
    #         kwargs["url"] = self.url
    #     if "headers" not in kwargs:
    #         kwargs["headers"] = self.headers
    #     if "method" not in kwargs:
    #         kwargs["method"] = self.method
    #     if "data" not in kwargs:
    #         kwargs["data"] = self.data
    #     if "json" not in kwargs:
    #         kwargs["json"] = self.json
    #     if "params" not in kwargs:
    #         kwargs["params"] = self.params
    #     if "files" not in kwargs:
    #         kwargs["files"] = self.files
    #     self.resp = self.session.request(**kwargs)
    #     return self.resp